<template>
  <div>
    <Card :datalist="list" :arr="color" />
    <Lines @sendData="getSonData" />
  </div>
</template>

<script>
import Card from './components/Card.vue'
import Lines from './components/LineCharts.vue'
import { HomeLines } from '@/api/charts.js'
export default {
  components: {
    Card,
    Lines
  },
  data() {
    return {
      list: [
        {
          id: 0,
          icon: 'icon-task',
          text: '总订单',
          name: 'totalOrder',
          num: ''
        },
        {
          id: 1,
          icon: 'icon-Dollar',
          text: '总销售额',
          name: 'totalAmount',
          num: ' '
        },
        {
          id: 2,
          icon: 'icon-icon-',
          text: '今日订单数',
          name: 'todayOrder',
          num: ''
        },
        {
          id: 4,
          icon: 'icon-renminbi',
          text: '今日销售额',
          name: 'totayAmount',
          num: ''
        }
      ],
      color: ['#1296db', '#d4237a', '#1296db', '#1afa28'],
      dataControl: {}
    }
  },
  created() {
    this.getData()
  },
  methods: {
    async getData() {
      const data = await HomeLines()
      // console.log(data)
      this.list.forEach(item => {
        for (const k in data) {
          if (k === item.name) {
            item.num = data[k]
          }
        }
      })
      // 把 x轴坐标的数据请求回来，并且传入到子组件
      const obj = this.dataControl
      // 先是根据请求，修改了x轴的数据，然后再渲染页面
      obj.option.xAxis.data = data.xData
      obj.option.series[0].data = data.amountData
      obj.option.series[1].data = data.orderData
      obj.myChart.setOption(obj.option)
    },
    getSonData(o) {
      this.dataControl = o
    }
  }
}
</script>

<style lang="less" scoped></style>
